package com.example.yk2021xm04.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yk2021xm04.model.RespBean;
import com.example.yk2021xm04.pojo.Project;
import com.example.yk2021xm04.query.ProjectQuery;
import com.example.yk2021xm04.service.IProjectService;
import com.example.yk2021xm04.service.IProjectUserService;
import com.example.yk2021xm04.utils.IDUtils;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZengQi
 * @since 2021-11-26
 */
@Controller
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private StringEncryptor stringEncryptor;

    @Resource
    private IProjectService projectService;

    @Resource
    private IProjectUserService projectUserService;

    /**
     * 项目管理主界面
     * @return
     */
    @RequestMapping("index")
    public String index(String action, Model model) {
        System.out.println(action);
        if("deleted".equals(action)) {
            model.addAttribute("action",action);
            return "project/project_deleted";
        } else if("private".equals(action)) {
            model.addAttribute("action",action);
            return "project/project_private";
        } else if("public".equals(action)) {
            model.addAttribute("action",action);
            return "project/project_public";
        } else {
            return "project/project";
        }
    }

    /**
     * 项目统计页面
     * @author ZengQi
     * @date 2021/12/19 0019 9:41
     * @return String
     */
    @RequestMapping("count")
    public String statistic() {
        return "project/project-statistics";
    }

    @RequestMapping("statistic")
    @ResponseBody
    public RespBean projectStatistic() {
        //创建时间分组 group by day
        List<Object> createTimeByDay = projectService.createTimeByDay();
        //一天中的创建项目数
        List<Object> createCountByDay = projectService.createCountByDay();

        //总项目数量
        Integer totalProjectCount = projectService.count();
        //今天的创建项目数量
        Integer ProjectCountInDay = projectService.ProjectCountInDay();

        Map<String, Object> res = new HashMap<>();
        res.put("num", createCountByDay);
        res.put("time", createTimeByDay);
        res.put("total", totalProjectCount);
        res.put("dayCount", ProjectCountInDay);
        return RespBean.success("项目统计数据返回成功",res);
    }

    /**
     * 添加更新项目页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateProjectPage")
    public String addOrUpdatePage(Integer id, Model model) {
        if (null!=id) {
            Project project = projectService.getById(id);
            System.out.println(project);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String beginTime = dateTimeFormatter.format(project.getBeginTime());
            String endTime = dateTimeFormatter.format(project.getEndTime());
            project.setProjectLeader(projectUserService.findProjectLeaderByCreateId(project.getCreateId()));
            model.addAttribute("project",project);
            model.addAttribute("beginTime",beginTime);
            model.addAttribute("endTime",endTime);
        }
        return "project/add_update";
    }

    /**
     * 返回用户参加的项目信息
     * @author ZengQi
     * @date 2021/12/14 0014 14:13
     * @param projectQuery
     * @return Map<Object>
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> projectList(ProjectQuery projectQuery) {
        return projectService.projectList(projectQuery);
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteProject(Integer[] ids) {
        projectService.deleteProject(ids);
        return RespBean.success("项目记录删除成功");
    }

    @RequestMapping("add")
    @ResponseBody
    public RespBean addProject(Project project) {
        projectService.addProject(project);
        return RespBean.success("项目记录添加成功");
    }


    @RequestMapping("update")
    @ResponseBody
    public RespBean updateProject(Project project) {
        projectService.updateProject(project);
        return RespBean.success("项目记录更新成功");
    }

    @RequestMapping("quit")
    @ResponseBody
    public RespBean quitProject(Integer id) {
        projectService.quitProject(id);
        return RespBean.success("退出成功");
    }

    /**
     * 项目详情界面
     * @author ZengQi
     * @date 2021/12/15 0015 21:49
     * @return String
     */
    @RequestMapping("detailPage")
    public String detailPage() {
        return "project/project_detail";
    }

    /**
     *
     * @author ZengQi
     * @date 2021/12/16 0016 19:34
     * @param id
     * @return Map<Object>
     */
    @RequestMapping("detail")
    @ResponseBody
    public Map<String, Object> detail(Integer id) {
        List<Map<String, Object>> detail;
        detail = projectUserService.detail(id);
        System.out.println(detail);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("count",result.size());
        result.put("data",detail);
        result.put("code",0);
        result.put("msg","");
        return result;
    }

    /**
     * 加入项目界面
     * @author ZengQi
     * @date 2021/12/15 0015 21:49
     * @return String
     */
    @RequestMapping("joinProjectPage")
    public String joinProjectPage() {
        return "project/project_join";
    }

    /**
     * 邀请链接界面
     * @author ZengQi
     * @date 2021/12/17 0017 15:54
     * @return String
     */
    @RequestMapping("invitePage")
    public String invitePage() {
        return "project/project_invite";
    }

    @RequestMapping("invite")
    @ResponseBody
    public RespBean invitePage(Integer id) {
        //生成邀请时间
        LocalDateTime inviteCodeCreateTime = LocalDateTime.now().withNano(0);
        String inviteUrl = "inviteCode="+id+"&inviteCodeCreateTime="+inviteCodeCreateTime;
        System.out.println("加密前："+inviteUrl);
        inviteUrl = stringEncryptor.encrypt(inviteUrl);
        System.out.println("加密后："+inviteUrl);
        return RespBean.success("链接生成成功",inviteUrl);
    }

    @RequestMapping("join")
    @ResponseBody
    public RespBean joinProject(String inviteUrl) {
        System.out.println("加密的邀请链接："+inviteUrl);
//        String inviteUrl2 = stringEncryptor.decrypt(inviteUrl);
//        System.out.println("解密后的邀请链接："+inviteUrl2);
        projectService.joinProject(inviteUrl);
        return RespBean.success("加入成功!");
    }
}
